package com.ddxcloud.common.utils;


import javax.servlet.http.HttpServletRequest;

public class IpUtil {

    public static String getClientIp(HttpServletRequest request) {
        // 获取代理服务器IP地址
        String proxyIp = request.getHeader("x-forwarded-for");
        // 如果存在代理服务器IP地址，则从中提取客户端真实IP地址
        if (proxyIp != null && proxyIp.length() != 0) {
            String[] ips = proxyIp.split(",");
            for (String ip : ips) {
                if (!"unknown".equalsIgnoreCase(ip)) {
                    return ip.trim();
                }
            }
        }
        // 如果不存在代理服务器IP地址，则直接获取远程IP地址
        return request.getRemoteAddr();
    }
}
